﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;
using CuteWebUI;

namespace Web
{
    public partial class EmpBasicInfoTab : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                string empId = Request.QueryString["empId"];
                if (!string.IsNullOrEmpty(empId) && empId !="undefined")
                {
                    ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                    Employee employee = ctx.Employees.Single(r => r.Id == int.Parse(empId));
                    EmpId.Value = empId;
                    Name.Value = employee.Name;
                    JobNo.Value = employee.JobNo;
                    JobNo.Disabled = true;
                    EName.Value = employee.EName;
                    imgPhoto.Src = @"~\Photoes\" + employee.PhotoName;
                    BornDate.Value = employee.BornDate.ToString("yyyy-MM-dd");
                    Sex.Value = employee.Sex;
                    Animal.Value = employee.Animal;
                    EduLevel.Value = employee.EduLevel;
                    Major.Value = employee.Major;
                    EduDegree.Value = employee.EduDegree;
                    EngLevel.Value = employee.EngLevel;
                    Cantonese.Value = employee.Cantonese;
                    OtherLanguage.Value = employee.OtherLanguage;
                    NativePlace.Value = employee.NativePlace;
                    NativePlaceType.Value = employee.NativePlaceType;
                    Nation.Value = employee.Nation;
                    TechTitle.Value = employee.TechTitle;
                    TechLevel.Value = employee.TechLevel;
                    PoliticsStatus.Value = employee.PoliticsStatus;
                    Height.Value = employee.Height.ToString();
                    Weight.Value = employee.Weight.ToString();
                    SightLeft.Value = employee.SightLeft.ToString();
                    SightRight.Value = employee.SightRight.ToString();
                    BloodType.Value = employee.BloodType;
                    Marriage.Value = employee.Marriage;
                    BirthStatus.Value = employee.BirthStatus;
                    Faves.Value = employee.Faves;
                    Tel.Value = employee.Tel;
                    TelExt.Value = employee.TelExt;
                    Mobile.Value = employee.Mobile;
                    HomeTel.Value = employee.HomeTel;
                    EMail.Value = employee.EMail;
                    QQ.Value = employee.QQ.ToString();
                    HomePage.Value = employee.HomePage;
                    MSN.Value = employee.MSN;
                    IdNo.Value = employee.IdNo;
                    Contacter.Value = employee.Contacter;
                    ContacterTel.Value = employee.ContacterTel;
                    InfoFrom.Value = employee.InfoFrom;
                    IdAddress.Value = employee.IdAddress;
                    HomeAddress.Value = employee.HomeAddress;
                    ShiSu.Value = employee.ShiSu;
                    Penal.Value = employee.Penal;
                    Fire.Value = employee.Fire;
                    Issue.Value = employee.Issue;
                    if (employee.PacticeDate!=null)
                    {
                        PacticeDate.Value = ((DateTime)employee.PacticeDate).ToString("yyyy-MM-dd");
                    }
                    if (employee.TrialDate != null)
                    {
                        TrialDate.Value = ((DateTime)employee.TrialDate).ToString("yyyy-MM-dd");
                    }
                    if (employee.DueDate != null)
                    {
                        DueDate.Value = ((DateTime)employee.DueDate).ToString("yyyy-MM-dd");
                    }
                    if (employee.LeaveDate != null)
                    {
                        LeaveDate.Value = ((DateTime)employee.LeaveDate).ToString("yyyy-MM-dd");
                    }
                    EmplyForm.Value = employee.EmplyForm;
                    LeaveCause.Value = employee.LeaveCause;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Employee employee;
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            if (String.IsNullOrEmpty(EmpId.Value))
            {
                employee=new Employee();
                employee.Password = "7C4A8D09CA3762AF61E59520943DC26494F8941B";//默认密码:123456
                ctx.Employees.InsertOnSubmit(employee);
            }
            else
            {
                employee = ctx.Employees.Single(r => r.Id == int.Parse(EmpId.Value));
            }
            employee.Name = Name.Value;
            employee.JobNo = JobNo.Value;
            employee.EName = EName.Value;
            employee.PhotoName = imgPhoto.Src.Replace(@"~\Photoes\", "");
            employee.BornDate = DateTime.Parse(BornDate.Value);
            employee.Sex = Sex.Value;
            employee.Animal = Animal.Value;
            employee.EduLevel = EduLevel.Value;
            employee.Major = Major.Value;
            employee.EduDegree = EduDegree.Value;
            employee.EngLevel = EngLevel.Value;
            employee.Cantonese = Cantonese.Value;
            employee.OtherLanguage = OtherLanguage.Value;
            employee.NativePlace = NativePlace.Value;
            employee.NativePlaceType = NativePlaceType.Value;
            employee.Nation = Nation.Value;
            employee.TechTitle = TechTitle.Value;
            employee.TechLevel = TechLevel.Value;
            employee.PoliticsStatus = PoliticsStatus.Value;
            if (!string.IsNullOrEmpty(Height.Value))
            {
                employee.Height = int.Parse(Height.Value);
            }
            if (!string.IsNullOrEmpty(Weight.Value))
            {
                employee.Weight = int.Parse(Weight.Value);
            }
            if (!string.IsNullOrEmpty(SightLeft.Value))
            {
                employee.SightLeft = double.Parse(SightLeft.Value);
            }
            if (!string.IsNullOrEmpty(SightRight.Value))
            {
                employee.SightRight = double.Parse(SightRight.Value);
            }
            employee.BloodType=BloodType.Value;
            employee.Marriage=Marriage.Value;
            employee.BirthStatus=BirthStatus.Value;
            employee.Faves=Faves.Value;
            employee.Tel=Tel.Value;
            employee.TelExt=TelExt.Value;
            employee.Mobile=Mobile.Value;
            employee.HomeTel=HomeTel.Value;
            employee.EMail = EMail.Value;
            if (!string.IsNullOrEmpty(QQ.Value))
            {
                employee.QQ = int.Parse(QQ.Value);
            }
            employee.HomePage=HomePage.Value;
            employee.MSN=MSN.Value;
            employee.IdNo=IdNo.Value;
            employee.Contacter=Contacter.Value;
            employee.ContacterTel=ContacterTel.Value;
            employee.InfoFrom=InfoFrom.Value;
            employee.IdAddress=IdAddress.Value;
            employee.HomeAddress=HomeAddress.Value;
            employee.ShiSu=ShiSu.Value;
            employee.Penal=Penal.Value;
            employee.Fire=Fire.Value;
            employee.Issue = Issue.Value;
            if (!string.IsNullOrEmpty(PacticeDate.Value))
            {
                employee.PacticeDate = DateTime.Parse(PacticeDate.Value);
            }
            if (!string.IsNullOrEmpty(TrialDate.Value))
            {
                employee.TrialDate = DateTime.Parse(TrialDate.Value);
            }
            if (!string.IsNullOrEmpty(DueDate.Value))
            {
                employee.DueDate = DateTime.Parse(DueDate.Value);
            }
            if (!string.IsNullOrEmpty(LeaveDate.Value))
            {
                employee.LeaveDate = DateTime.Parse(LeaveDate.Value);
            }
            employee.EmplyForm=EmplyForm.Value;
            employee.LeaveCause = LeaveCause.Value;

            ctx.SubmitChanges();
            ClientScript.RegisterStartupScript(this.GetType(), "", "window.returnValue=" + employee.Id + ";window.close();", true);
        }

        protected void Uploader_File(object sender, UploaderEventArgs args)
        {
            string m_path = @"~\Photoes\";
            m_path = Server.MapPath(m_path);
            int dotIndex = args.FileName.LastIndexOf('.');
            string fileExt = args.FileName.Substring(dotIndex);

            string saveFileName = args.FileName;
            string savePath = m_path + saveFileName;
            string fileName = args.FileName.Substring(0, dotIndex);
            int i = 1;
            while (System.IO.File.Exists(savePath))
            {
                i++;
                saveFileName = fileName + "_" + i + fileExt;
                savePath = m_path + saveFileName;
            }
            args.MoveTo(savePath);
            imgPhoto.Src = @"~\Photoes\" + saveFileName;
        }
    }
}